<?php

//require './php_users.php';

$roles = procedure_call00("ROLESSISTEMA");
$roles_ = Array("DEFAULT");
$privilegios = procedure_call00("PRIVSISTEMA");
$privilegios_ = Array("DEFAULT");
$tablas = procedure_call00("TODASTABLAS"); 
$tablas_ = Array();

foreach ($roles as $r)
    array_push($roles_, implode($r));
foreach ($privilegios as $p)
    array_push($privilegios_, implode($p));
foreach($tablas as $t0)
{ 
    $tv = implode($t0);
    array_push($privilegios_,"SELECT ON ".$tv);
    array_push($privilegios_,"DELETE ON ".$tv);
    array_push($privilegios_,"INSERT ON ".$tv);
    array_push($privilegios_,"UPDATE ON ".$tv);
    array_push($privilegios_,"ALL ON ".$tv);
}


$str = '<div id="pop_mod_rol" class="divScroll">';
    $str.=          comboboxR(Array("NOT IDENTIFIED","IDENTIFIED BY","IDENTIFIED EXTERNALLY","IDENTIFIED GLOBALLY"),"IDENTIFIEDM");
    $str.=          inputBonitoR("claveRoleM", "Inserte una Clave", "password", "rol-claveM");
    $str.=          inputBonitoR("claveRole2M", "Confirme su Clave", "password","rol-clave2M");    
    

//--aqui va las tablas
$str.= '<div class="row">';
$str.= '<div class="form-group col-md-6"><div class="row">';
$str.= selectBox000($roles_, "cmbRoles00", "ROLES", "col-md-6") . '<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow(' . "'cmbRoles00'" . ')"></span></div>';
$str.= Tabla000(Array("Row", "Role"), Array(), "", "tablamodrol");
$str.= '</div>';
$str.= '<div class="form-group col-md-6"><div class="row">';
$str.= selectBox000($privilegios_, "cmbPermisos00", "Permisos", "col-md-6") . '<span class="glyphicon glyphicon-plus-sign col-md-1" onclick="addNuevaRow(' . "'cmbPermisos00'" . ')"></span></div>';
$str.= Tabla000(Array("Row", "Privilege"), Array(), "", "tablamodpriv");
$str.= '</div></div>'; 
$str.= '</div>';//29
echo $str;


function Label00($valor) {
    $str = '<span class="label label-primary">' . $valor . '</span>';
    return $str;
}

function Label1($valor) {
    $str = '<span class="label label-primary">' . $valor . '</span>';
    return $str;
}

function selectBox000($opciones, $id, $labelName, $class) {
    $str = '<div class="form-group ' . $class . '">';
    $str .= '<div class="row-fluid">';
    $str .= Label00($labelName);
    $str .= '<select class="selectpicker" id="' . $id . '" data-style="btn-primary">';
    foreach ($opciones as $value)
        $str .= '<option>' . $value . '</option>';
    $str.= '</select>';
    $str .= '</div></div>';
    return $str;
}

function Tabla000($theads, $tbodys, $onclick, $id) {
    $cantCol = 0;
    $cantIns = 0;

    $str = '<div class="table-responsive"> ';
    $str .= '<table class="table table-striped table-fixed-header" id="' . $id . '">'; //  table table-bordered table-fixed-header
    $str.= ' <thead class="header">';

    //lleno thead
    $resultado = count($theads);
    $bool = true;
    foreach ($theads as $thead) {
        if ($bool) {
            $str.= '<tr>';
            $bool = false;
        }
        $str.='<th>' . $thead . '</th>';
        $cantCol++;
        if ($cantCol == $resultado) {
            $str.='</tr>';
        }
    }
    $str.='</thead>';

    //lleno tbody
    $str.='<tbody>';
    foreach ($tbodys as $tbody) {
        if ($cantIns == 0) {
            $cantIns++;
            $str.='<tr onclick="' . $onclick . '" >';
            $str.='<td>' . $tbody . '</td>';
        } elseif ($cantIns != $cantCol && $cantIns <= $cantCol) {
            $cantIns++;
            $str.='<td>' . $tbody . '</td>';
            if ($cantIns == $cantCol) {
                $str.='</tr>';
                $cantIns = 0;
            }
        }
    }
    $str.='</tbody>';
    $str.='</table>';
    $str.='</div>';
    return $str;
//$divPrincipal->echo ($str);
}

function procedure_call00($pr) {
    require("./Conn.php");
    $array = Array();
    if (!$conn) {
        $m = oci_error();
        trigger_error(htmlentities($m['message']), E_USER_ERROR);
    }
    $curs = oci_new_cursor($conn);
    $stid = oci_parse($conn, "begin " . $pr . "(:cursorAux); end;");
    //$stid = oci_parse($conn, "begin PRC001(:cursorAux); end;");
    oci_bind_by_name($stid, ":cursorAux", $curs, -1, OCI_B_CURSOR);
    oci_execute($stid);
    oci_execute($curs);  // Execute the REF CURSOR like a normal statement id 
    while ($row = oci_fetch_array($curs, OCI_ASSOC)) {
        array_push($array, $row);
    }
    return $array;
}
function inputBonitoR($name, $des,$type,$class)
{
    $str ='<div class="form-group '.$class.'">';
    $str .= '<div class="row-fluid">';
    $str .=     Label1($name);
    $str.=  '<input type="'.$type.'"placeholder="'.$des.'" id="id_'.$name.'">';
    $str.='</div></div>'; 
    return $str;
} 
function comboboxR($listaValores,$id)
{
    $str='<div class="form-group">';
    $str.=  '<label for="id_'.$id.'_">'.$id.' de la clave :</label>'; 
    $str.=  '<select class="form-control" id="id_'.$id.'_">'; 
        foreach($listaValores as $v)
            $str.='<option>'.$v.'</option>'; 
    $str.=  ' </select>';
    $str.='</div>';
    return $str;
}  